/*
 * This file is part of Euphrat.
 *
 * Copyright © 2012 Bart Verhoeven
 *
 * Euphrat is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Euphrat is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Euphrat. If not, see <http://www.gnu.org/licenses/>.
 */
package org.euphrat.client.model;

import com.google.common.base.Objects;
import com.google.common.collect.Sets;
import static com.google.common.base.Preconditions.checkNotNull;

import java.util.Set;

/**
 * A utility class to implement {@link Observable}.
 *
 * @author Bart Verhoeven
 */
public class ObservableSupport {

  private final Observable source;
  private final Set<Observer> observers;

  /**
   * Creates a new support instance.
   *
   * @param source the source object of all changes
   * @throws NullPointerException source is {@code null}
   */
  public ObservableSupport(Observable source) {
    checkNotNull(source, "The source of a change can never be null.");
    this.source = source;
    observers = Sets.newHashSet();
  }

  /**
   * Registers an observer for the source observable. The observer monitors all
   * exposed properties of the observable. An observer may be added multiple
   * times, but will only be notified once of a particular change to the source.
   *
   * @param observer the observer to register
   */
  public void register(Observer observer) {
    if (observer != null) {
      observers.add(observer);
    }
  }

  /**
   * Unregisters an observer from the source observable. The observer stops
   * monitoring all exposed properties.
   *
   * @param observer the observer to unregister
   */
  public void unregister(Observer observer) {
    observers.remove(observer);
  }

  /**
   * Notifies all registered observers of a change. A change event is only fired
   * if the old and new value differ.
   *
   * @param property the property that changed
   * @param oldValue the old property value
   * @param newValue the new property value
   */
  public void fireObservableChange(String property,
                                   Object oldValue,
                                   Object newValue) {

    // Ignore invalid properties.
    if (property == null || property.trim().isEmpty()) {
      return;
    }

    // Identical values do not indicate a change, ignore it.
    if (Objects.equal(oldValue, newValue)) {
      return;
    }

    // Put together the observable change.
    ObservableChange change = new ObservableChange(
      source, property, oldValue, newValue
    );

    fireObservableChangeImpl(change);
  }

  /**
   * Unconditionally notifies all registered observers of a change.
   *
   * @param change the change the observers should be notified of
   */
  protected void fireObservableChangeImpl(ObservableChange change) {
    for (Observer observer : observers) {
      observer.notifyOf(change);
    }
  }
}
